package com.myproject.weather.weatherInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	public DBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String s = "CREATE TABLE weatherdata(cityid TEXT, city TEXT, date_y TEXT, temp1 TEXT,"
				+ "temp2 TEXT,temp3 TEXT,temp4 TEXT,temp5 TEXT,temp6 TEXT,weather1 TEXT,weather2 TEXT,"
				+ "weather3 TEXT,weather4 TEXT,weather5 TEXT,weather6 TEXT,wind1 TEXT,wind2 TEXT,wind3 TEXT,"
				+ "wind4 TEXT,wind5 TEXT,wind6 TEXT,index_n TEXT,index_d VARCHAR(120),index48 TEXT,index48_d VARCHAR(120),"
				+ "index_uv TEXT,index48_uv TEXT,index_xc TEXT,index_tr TEXT,index_co TEXT,index_cl TEXT,"
				+ "index_ls TEXT,index_ag TEXT);";

		String sk = "CREATE TABLE weatherdatask(cityid TEXT,city TEXT,temp TEXT,wind TEXT,sd TEXT,time TEXT);";
		db.execSQL(s);
		db.execSQL(sk);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void updateWeatherSKData(WeatherSKData weatherSKData) {
		SQLiteDatabase writeable = getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("cityid", weatherSKData.cityid);
		cv.put("city", weatherSKData.city);
		cv.put("temp", weatherSKData.temp);
		cv.put("wind", weatherSKData.wind);
		cv.put("sd", weatherSKData.sd);
		cv.put("time", weatherSKData.time);

		try {
			writeable.beginTransaction();
			writeable.delete("weatherdatask", "cityid=?", new String[] { weatherSKData.cityid });
			writeable.insert("weatherdatask", null, cv);
			writeable.setTransactionSuccessful();
		} finally {
			writeable.endTransaction();
			writeable.close();
		}
	}

	public void updateWeatherData(WeatherData weatherData) {
		SQLiteDatabase writeable = getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("cityid", weatherData.cityid);
		cv.put("city", weatherData.city);
		cv.put("date_y", weatherData.date_y);
		cv.put("temp1", weatherData.temp1);
		cv.put("temp2", weatherData.temp2);
		cv.put("temp3", weatherData.temp3);
		cv.put("temp4", weatherData.temp4);
		cv.put("temp5", weatherData.temp5);
		cv.put("temp6", weatherData.temp6);
		cv.put("weather1", weatherData.weather1);
		cv.put("weather2", weatherData.weather2);
		cv.put("weather3", weatherData.weather3);
		cv.put("weather4", weatherData.weather4);
		cv.put("weather5", weatherData.weather5);
		cv.put("weather6", weatherData.weather6);
		cv.put("wind1", weatherData.wind1);
		cv.put("wind2", weatherData.wind2);
		cv.put("wind3", weatherData.wind3);
		cv.put("wind4", weatherData.wind4);
		cv.put("wind5", weatherData.wind5);
		cv.put("wind6", weatherData.wind6);
		cv.put("index_n", weatherData.index_n);
		cv.put("index_d", weatherData.index_d);
		cv.put("index48", weatherData.index48);
		cv.put("index48_d", weatherData.index48_d);
		cv.put("index48_uv", weatherData.index48_uv);
		cv.put("index_xc", weatherData.index_xc);
		cv.put("index_tr", weatherData.index_tr);
		cv.put("index_co", weatherData.index_co);
		cv.put("index_cl", weatherData.index_cl);
		cv.put("index_ls", weatherData.index_ls);
		cv.put("index_ag", weatherData.index_ag);

		try {
			writeable.beginTransaction();
			writeable.delete("weatherdata", "cityid=?", new String[] { weatherData.cityid });
			writeable.insert("weatherdata", null, cv);
			writeable.setTransactionSuccessful();
		} finally {
			writeable.endTransaction();
			writeable.close();
		}
	}

	public WeatherSKData getWeatherSKData(String cityId) {
		String[] columns = { "cityid", "city", "temp", "wind", "sd", "time" };
		Cursor cursor = this.getReadableDatabase().query("weatherdatask", columns, "cityid=?",
				new String[] { cityId }, null, null, null);
		cursor.moveToFirst();
		WeatherSKData weatherData = new WeatherSKData();
		if (cursor.getCount() == 0) {
			return weatherData;
		}
		weatherData.cityid = cursor.getString(0);
		weatherData.city = cursor.getString(1);
		weatherData.temp = cursor.getString(2);
		weatherData.wind = cursor.getString(3);
		weatherData.sd = cursor.getString(4);
		weatherData.time = cursor.getString(5);
		return weatherData;
	}

	public WeatherData getWeatherData(String cityId) {
		String[] columns = { "cityid", "city", "date_y", "temp1", "temp2", "temp3", "temp4", "temp5",
				"temp6", "weather1", "weather2", "weather3", "weather4", "weather5", "weather6", "wind1",
				"wind2", "wind3", "wind4", "wind5", "wind6", "index_n", "index_d", "index48", "index48_d",
				"index_uv", "index48_uv", "index_xc", "index_tr", "index_co", "index_cl", "index_ls",
				"index_ag" };
		Cursor cursor = this.getReadableDatabase().query("weatherdata", columns, "cityid=?",
				new String[] { cityId }, null, null, null);
		cursor.moveToFirst();
		WeatherData weatherData = new WeatherData();
		if (cursor.getCount() == 0) {
			return weatherData;
		}
		weatherData.cityid = cursor.getString(0);
		weatherData.city = cursor.getString(1);
		weatherData.date_y = cursor.getString(2);
		weatherData.temp1 = cursor.getString(3);
		weatherData.temp2 = cursor.getString(4);
		weatherData.temp3 = cursor.getString(5);
		weatherData.temp4 = cursor.getString(6);
		weatherData.temp5 = cursor.getString(7);
		weatherData.temp6 = cursor.getString(8);
		weatherData.weather1 = cursor.getString(9);
		weatherData.weather2 = cursor.getString(10);
		weatherData.weather3 = cursor.getString(11);
		weatherData.weather4 = cursor.getString(12);
		weatherData.weather5 = cursor.getString(13);
		weatherData.weather6 = cursor.getString(14);
		weatherData.wind1 = cursor.getString(15);
		weatherData.wind2 = cursor.getString(16);
		weatherData.wind3 = cursor.getString(17);
		weatherData.wind4 = cursor.getString(18);
		weatherData.wind5 = cursor.getString(19);
		weatherData.wind6 = cursor.getString(20);
		weatherData.index_n = cursor.getString(21);
		weatherData.index_d = cursor.getString(22);
		weatherData.index48 = cursor.getString(23);
		weatherData.index48_d = cursor.getString(24);
		weatherData.index48_uv = cursor.getString(25);
		weatherData.index_xc = cursor.getString(26);
		weatherData.index_tr = cursor.getString(27);
		weatherData.index_co = cursor.getString(28);
		weatherData.index_cl = cursor.getString(29);
		weatherData.index_ls = cursor.getString(30);
		weatherData.index_ag = cursor.getString(31);
		return weatherData;
	}
}
